<script setup lang="ts">
import { ref, markRaw } from "vue";
import { useColumns } from "./compower_columns";
import Plus from "~icons/ep/plus";
import Delete from "~icons/ep/delete";
import "plus-pro-components/es/components/dialog-form/style/css";
import "plus-pro-components/es/components/search/style/css";
import { addProductInfo, setProductOrder, getOrderInfo } from "@/api/finance";
import {
  type PlusColumn,
  type FieldValues,
  PlusSearch,
  PlusDialogForm
} from "plus-pro-components";
import { type Action, ElMessageBox, ElMessage } from "element-plus";
const edit = ref(false);

const rules = {
  status: [
    {
      required: true,
      message: "状态必选"
    }
  ]
};

const searchColumns: PlusColumn[] = [
  {
    label: "搜索",
    width: 200,
    prop: "keyWord",
    valueType: "copy",
    tooltip: "请输入要查询的关键字"
  },
  {
    label: "订单状态",
    width: 200,
    prop: "status",
    valueType: "select",
    options: [
      {
        label: "进行中",
        value: 1
      },
      {
        label: "已结束",
        value: 2
      },
      {
        label: "需要在签约",
        value: 4
      }
    ]
  },
  {
    label: "权重值",
    width: 200,
    prop: "weight",
    valueType: "input-number",
    tooltip: "请输入大于权重值的订单"
  }
];

const formColumns: PlusColumn[] = [
  {
    label: "状态",
    prop: "status",
    valueType: "radio",
    options: [
      {
        label: "收益中",
        value: "1"
      },
      {
        label: "已结束",
        value: "2"
      }
    ]
  }
];

const visible = ref(false);
const values = ref<FieldValues>({});
const props = defineProps({
  type: {
    type: Number,
    default: 4
  },
  add: {
    type: Boolean,
    default: false
  },
  edit: {
    type: Boolean,
    default: false
  },
  delete: {
    type: Boolean,
    default: false
  }
});
const {
  loading,
  columns,
  dataList,
  pagination,
  loadingConfig,
  adaptiveConfig,
  onKeywordChange,
  loadData,
  onSizeChange,
  onCurrentChange
} = useColumns(props.type);
if (!props.delete && !props.edit) {
  columns.pop();
}
function deleteClick(row) {
  ElMessageBox.confirm("是否删除", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    draggable: true,
    type: "error",
    icon: markRaw(Delete),
    callback: async (action: Action) => {
      // 在这里执行删除操作
      if (action === "confirm") {
        console.log("执行删除操作");
        try {
          const params = {
            type: props.type,
            orderId: row.id,
            isDelete: 1
          };
          const res = await setProductOrder(params);
          loadData();
          ElMessage.success("删除成功");
        } catch (error) {
          console.log(error, "error");
        }
      }
    }
  });
}
const editClick = async row => {
  console.log(
    "%crow===编辑>>>: ",
    "color: MidnightBlue; background: Aquamarine; font-size: 20px;",
    row
  );
  try {
    const res = await getOrderInfo(row.id, props.type);
    Object.assign(values.value, res.data);
    // values.value = { ...res.data };
    console.log(
      "%cres===>>>",
      "color: MidnightBlue; background: Aquamarine; font-size: 20px;",
      "返回的",
      res,
      values.value
    );
    edit.value = true;
    visible.value = true;
  } catch (error) {
    console.log(error, "error");
  }
};
const add = () => {
  console.log("%c点击了添加商品>>>: ", "color: MidnightBlue; font-size: 20px;");
  ElMessage.info("暂未开发该功能");
  return;
  edit.value = false;
  values.value = {};
  visible.value = true;
};
const confirmLoading = ref(false);
const handleSubmit = async (value: FieldValues) => {
  const updatedValues = { ...values.value };
  confirmLoading.value = true;

  if (edit.value) {
    try {
      const params = {
        type: props.type,
        orderId: updatedValues.id,
        status: updatedValues.status
      };
      await setProductOrder(params);
      visible.value = false;
      confirmLoading.value = false;
      formRef.value?.formInstance?.resetFields();
      loadData();
      ElMessage.success("修改成功");
    } catch (error) {
      confirmLoading.value = false;
      console.log(error, "error");
    }
  } else {
    try {
      await addProductInfo(updatedValues);
      visible.value = false;
      confirmLoading.value = false;
      formRef.value?.formInstance?.resetFields();
      loadData();
    } catch (error) {
      confirmLoading.value = false;
      console.log(error, "error");
    }
  }
};

const handleSearch = (value: any) => {
  console.log(value, "search");
  onKeywordChange(value);
};
const handleReset = value => {
  console.log("reset", value);
  onKeywordChange(value);
};
const handleChange = (value: any, target: any) => {
  console.log("change", value);
  if (target.prop == "keyWord" || target.prop == "weight") {
    return;
  }
  onKeywordChange(value);
};
const defaultSearchState = ref({});
const formRef = ref();
</script>

<template>
  <div class="flex flex-col">
    <ElPageHeader :icon="null">
      <template #title>
        <h3>订单管理</h3>
      </template>
      <template v-if="props.add" #extra>
        <div class="flex flex-row items-end justify-end mb-4">
          <el-button type="primary" @click="add">
            <el-icon :size="15" color="#fff">
              <IconifyIconOffline :icon="Plus" />
            </el-icon>
            添加订单
          </el-button>
        </div>
      </template>
      <PlusSearch
        :defaultValues="defaultSearchState"
        class="mt-5"
        :columns="searchColumns"
        :hasUnfold="false"
        @reset="handleReset"
        @search="handleSearch"
        @change="handleChange"
      />
    </ElPageHeader>
    <pure-table
      ref="tableRef"
      showOverflowTooltip
      border
      adaptive
      class="mt-2"
      :adaptiveConfig="adaptiveConfig"
      row-key="id"
      alignWhole="center"
      :loading="loading"
      :loading-config="loadingConfig"
      :data="dataList"
      :columns="columns"
      :pagination="pagination"
      @page-size-change="onSizeChange"
      @page-current-change="onCurrentChange"
    >
      <template #status="{ row }">
        <el-tag v-if="row.status == '1'" type="success">收益中</el-tag>
        <el-tag v-else-if="row.status == '2'" type="danger">已结束</el-tag>
        <el-tag v-else type="danger">未知状态</el-tag>
      </template>
      <template #cycle="{ row }">
        <span>{{ row.cycle }}/{{ row.countcycle }}</span>
      </template>
      <template #operation="{ row }">
        <el-button
          v-if="props.edit"
          type="primary"
          size="small"
          @click="editClick(row)"
          >编辑</el-button
        >
        <el-button
          v-if="props.delete"
          type="danger"
          size="small"
          @click="deleteClick(row)"
        >
          删除
        </el-button>
      </template>
    </pure-table>
    <PlusDialogForm
      ref="formRef"
      v-model:visible="visible"
      v-model="values"
      :form="{ columns: formColumns, rules, labelWidth: 120 }"
      :dialog="{ confirmLoading }"
      :title="edit ? '编辑订单' : '添加订单'"
      @confirm="handleSubmit"
    />
  </div>
</template>

<style scoped>
::deep .wot-message-box__icon--warning {
  --el-messagebox-color: red;
  color: red !important;
  fill: red !important;
}
</style>
